package com.eatme.core.design.pattern.creational.singleton;

/**
 * 延迟初始化
 * 饱汉模式(懒汉模式)–双重加锁检查DCL（Double Check Lock）
 */
public class LazyDoubleCheckSingleton {

    // volatile 保证数据可见性
    private volatile static LazyDoubleCheckSingleton lazyDoubleCheckSingleton =null;


    //私有构造函数
    private LazyDoubleCheckSingleton() {

    }

    public static LazyDoubleCheckSingleton getInstance() {

        //1、检查实例是否存在
        if (lazyDoubleCheckSingleton==null) {

            //同步块，线程安全的创建实例
            synchronized (LazyDoubleCheckSingleton.class) {

                //2、再次检查实例是否存在，如果不存在才真的创建实例
                if (lazyDoubleCheckSingleton==null) {

                    lazyDoubleCheckSingleton = new LazyDoubleCheckSingleton();
                    //1.分配内存给这个对象
                    //3.设置lazyDoubleCheckSingleton 指向的刚分配的内存地址
                    //2.初始化这个对象
//                    intra-thread semantics
//                    ---------------//3.设置lazyDoubleCheckSingleton 指向的刚分配的内存地址
                    //4.初次访问对象
                }
            }
        }
        return lazyDoubleCheckSingleton;
    }
}
